Helpful Information
 
 
Category: Flash Help
Simulate "real-time" updates in Flash using PHP-sockets or some other method?

This is from a question I posted earlier in the PHP forum, but with a horrible subject! Also, this is sort-of a PHP question, but it really applies to integrating functionality into Flash projects, so that's why I put it here.

I'm using PHP with a Flash (swf) front-end, but I want to have a some-what "real-time" update of data provided to the front-end. So, I guess my real question is this: do I need to use the limited "sockets"-capabilities of PHP to connect with Flash, or should I just set my Flash to constantly poll my PHP program to simulate "real-time" access? I'm concerned about overloading my webserver with accesses, and honestly I'm not even sure if using sockets is really the way to go since I've never used them before. Any thoughts or ideas?

Thanks!
TotalMonkey!

Heres is a way to update flash in realtime. There is a working example with the tutorial. http://www.snowvids.com/PerlTut/PerlWriteVarsToText.html

Well,...

I thank you, DaveH, but that's not really what I meant by simulating real-time. I guess I should clarify more. What I mean is to have a SWF that will constantly poll a DB or flat-file at regular intervals so that the SWF movie can be updated in what appears to be a "real-time" fashion, like for multiplayer games or chat and instant message clients and the like. I want users to have the impression that they are constantly connected without really have a persistent link, unless of course I can have a persistent link to the server that doesn't tie it up or overuse (read "abuse") it any way.

To be honest, I'm not sure there really is any way to do it, but I'm hoping that at least someone who knows about this kind of thing (with an understanding of both Flash and PHP and web servers in general) can tell whether it's possible or not.

Thanks,
TotalMonkey

If I understand you correctly, then the chat fla that was included in Flash 4's tutorials/samples did what you're asking (think Flash 5 works differently though). It had an action on one of its frames to submit a request to the server (could be any script or a flat file, whatever) and loaded the returned variables. I think the demo file came with an ASP script file, but you could easily adapt it to PHP of course.

When I got Flash 4 a couple of years ago I put that file up on a free server and it worked, but found it was 'locking up' quite regularly. And that was with no more than about 6 or 7 users at any one time. But that could well have been a crap server and any decent one should be able to manage a very high number of simultaneous hits without any problems.

Now, can I find the sample file anywhere? No. Must be out there somewhere though.

Originally posted by dang65
If I understand you correctly, then the chat fla that was included in Flash 4's tutorials/samples did what you're asking (think Flash 5 works differently though). It had an action on one of its frames to submit a request to the server (could be any script or a flat file, whatever) and loaded the returned variables...

When I got Flash 4 a couple of years ago I put that file up on a free server and it worked, but found it was 'locking up' quite regularly. And that was with no more than about 6 or 7 users at any one time. But that could well have been a crap server and any decent one should be able to manage a very high number of simultaneous hits without any problems.

dang65,
I'm pretty sure I know which tutorial you're talking about, but I think that has an action that checks the flat-file every second or so, and if we're talking about 20-30 (or 200-300) connects, reads, and sends every second...

I'm wondering, is there's a better way to do that? Is there a way to do persistent links that don't cost so much in the way of server activity? Can sockets (or anything else I don't fully understand yet) solve this problem?

Thanks for the reply, dang65; I think we're getting somewhere.

TotalMonkey

I found the answer to this. Here is the text to a similar thread I have since erased (since it was pretty much a duplicate subject!)


Originally posted by TotalMonkey
Re: How do you do "multiplayer" in Flash without hammering the server??

I have a flash file with a frame action that, after a couple frames (2 or 3) reads variables from a php program (which does a database read) and then "GoToAndPlay"s the first frame.

The problem is that this method casues the web server to seriously lag (big time, like 99% cpu and memory use). I realize that methods i've used are intrinsically taxing, but how else can I track the screen positions of two online users so that the movie can display accurate, up-to-date (to the half-second) data without such a tremendous hammering to the web server?

Can anyone help? Does this question belong in a different forum?

:confused:

Thanks,
TotalMonkey! ... and ...

Originally posted by TotalMonkey
Re: I found the answer...

I went to www.flashkit.com and asked my question there, and apparently the method I was using is (as I expected) not the preferred method for polling my PHP script. It looks like what I need to do is use "XML Socket Connections" to pass data back and forth in a "real-time" fashion.

These XML Socket Connections are implemented through a set of Flash 5 functions utilizing an "XMLSocket" object in Flash, and on the back-end using an (surprisingly enough) XML Socket server, which allows Flash to open up these socket connections and then (passively? actively?) "listen" to the server for "broadcast" messages. I'm in the process of researching these methods and technologies and have ordered the book recommended on the subject, "Flash Games Studio" from FriendsOfEd/Wrox publishing.Again, sorry for the double post...

Enjoy!
TotalMonkey

No you don't need to mess with sockets or any of that. Most PHP Flash Chat programs and live counters just use loadVariables() over and over to make HTTP calls to some PHP script. But then again it depends on what you're doing. If it's a small app then don't worry about it, if you want some huge multi-user application, then you would look into the XML sockets and what not.

I've discovered through experimentation and discussion with others that using LoadVars/LoadVarsNum with PHP just won't cut it. I have a machine setup on a LAN at home to act specifically as a web server, and the Flash-PHP test I tried cranked my CPU usage up to 99% and lagged both sides big-time -- that's with only one user/connection! So this is definitely not the way to go. (Add database accesses to that and you completely tie up the server.)

So, I asked around FlashKit and soon discovered that the most effective way to do this is to use Flash 5's XMLSocket object with a socket server on the server side (for best results - i.e. performance - built specifically for the Flash app you've built). As far as I understand, socket servers are just programs that allow persistant TCP or UDP connections to client programs.

Now, I'm no expert at socket programming, but there's quite a bit of info on the web about it. My next major concern was - what language to use? I've asked around and was told that C-based socket servers (when coded efficiently of course) typically have the best speed and performance, but I'm not too sure I can run executables from my host. A PHP-based socket server would do well for me, but the only responses to questions concerning tutorials or explanations have pointed me to the PHP manual. However, the only help the PHP manual provides for PHP socket programming is a non-working example and a *PHP does it the same as C*statement (which leads me to think that maybe I should just learn sockets in C). The user comments were somewhat helpful, but not exactly enough for a beginner.

So, that's my dilemma. I've bought an excellent Flash book called Flash Games Studio by friendsofed.com (bought it for less at Amazon.com), but it doesn't go into server programming, only Flash and ActionScript. I'm left with the realization that I'll probably have to learn to write a socket server in C, and maybe port that knowledge to PHP if it's worth it.

I'm open to discussion if anyone has comments, issues or questions. And by the way, thanks for the responses from DaveH, dang65, and JeffCT.

Thanks again,
TotalMonkey!

monkey
have you looked at generator or swish?

everyone I know has taken the cheap route and used php to get the info from the dB, format it so flash can read it, and write a text file on the server. Flash then grabs the text file on a regular interval and updates the movie that way.

But i think that generator (if you can afford it) is the solution that would make the most effective use of the server processor and would eliminate as many possible problems too. Swis is the cgi based alternative to generator but i think that some of it's capabilities are limited.

as for socket programming- way over my noggin (for right now).
hope you find something!

just spotted this right after i replied- haven't had the chance to read it through so I am not sure if it is what you are looking for but the name sure sounds helpful!

have a look and let us know if it is of any relevance!

http://hotwired.lycos.com/webmonkey/multimedia/shockwave_flash/tutorials/tutorial9.html :)

TotalMonkey:

I would have to say I think something is definitely not right with your setup, and it doesn't have anything to do with using loadVariables(). I have used the loadVariables() method with dynamic Flash applications and gotten over 100 concurrent users simulataneously on minimal hardware (PII 400Mhz, 128MB RAM). You need to find the real bottleneck of your program and not immediately assume it is Flash, because think about it - via loadVariables() you are just making an HTTP connection over and over so if that were really the problem in itself, your same setup would shoot the CPU usage up to 99% if someone were just browsing a web page off of that server (which is essentially the same thing).

I have the same setup as you. I used one machine as a client machine and wrote a script to mimic someone using the flash frontend (send HTTP requests about every 5-7 seconds for each user). Another machine acted as the web server. I was able to get up to over 100 concurrent users numerous times with the server not at 99% usage.

I realize there are variables involved in setups and that there could always be a difference, but I am convinced that is not what is causing you to only be able to support 1 user and I have done over 100.

Check for bottlenecks in your code, mainly database issues if you use MySQL. There could be a missing indexes in your database, or something else. I wouldn't give up on loadVariables() just yet, I assure you that you can support Many concurrent users simulateously via that method.

What is your code like? If you are getting that poor of performance with 1 user, XML sockets aren't going to help much, that's not the bottleneck of your code.

TotalMonkey:

Were you able to make any progress on this issue?

Well, yes and no...

There definitely a bottleneck between Flash and the database, but I think. I'm still analyzing the system, but if the database access is the problem then I've got a big problem.

Hm. Well let me know of your progress. I'm absolutely positive though, that you can support many concurrent users with the loadVariables() method, it's really not all that slow.

I'm not at my home computer now, but I think that my performance issues arise from the fact that, in order to simulate a multi-user "realtime" environment (i.e. update position information for 2 simultaneous players fast enough to trick the eye into thinking that it's "realtime"), my Flash movie needs to check the database (actually select then update) 4 times per second (3 frame intervals at 12fps). So not only is each player (i.e. each independent Flash movie) hitting the webserver 4 times per second, the DB is also getting 4 I/Os per second (with 2 queries per hit)!
The problem is, that there's not really another way of "broadcasting" all player positions to all players quickly without using a socket server. This would alleviate the need to constantly hit a DB or flat file for random access (since each individual Flash movie would store the info broadcast by the socket server). HTTP just doesn't do what I need in terms of speed.

Well that changes things then I would say. Four updates per second and instant updates would probably need the socket server to work well with many users. My application contacted the database about once per 6-8 seconds, and handled 100+ concurrent connections. So 12-18 http requests per second (5-6 queries per request) is about what it handled, but in your case that would only be a 5 or so users. The updates weren't made to be completely real time (instant) either, just needed to get info from the server. I'd still check for bottlenecks at the server end first, because minimal hardware should still be able to do 12+ requests per second, especially on a LAN.

Well,
I've finished my analysis, and it turns out my webservers (both Xitami on a 650MHz AMD Athlon WinPC and Apache on a 233MHz AMD K6-2 Linux) are just unable to handle the tremendous pounding that my Flash movie slams it with (between 4-6 hits per second with DB I/O). Evidence showed that the CPU and memory resources were maxxed out (both machines) using the 4-6 hits/sec movie both with and without database access, while a 1 hit every 4 seconds movie showed only a little over normal (i.e. no http hits) resource use for both with and without database access.

I really should have provided more detail in the explanation of my original question, but it took me some pretty deep delving into the subject (i had to read a few chapters on C network programming) in order to really define what I was trying to do vs. what I need.

Thanks, Jeff for sticking with me through this. I'll post my results to this thread when I've finished my little project for anyone who's interested in seeing how I got everything to work (assuming I really can! ;) ).

Later,
TotalMonkey!

Maybe it's because you're using Xitami on Windows...
I was using Apache on Red Hat Linux 7.1, handling 15+ requests per second (5-6 database accesses per request) with no problem, minimal hardware. I was using MySQL as the backend.

You may want to look into Java for your XML socket server. With Java you could toss together a server in minutes. Many beginners' Java books will have you make a server as one of the examples, so it's very easy. Plus it would be platform independent. I'd just do that.

thanks for the ideas, i'll check it out

TotalMonkey










privacy (GDPR)